package com.fanbai.score.common.activity;

import android.os.Handler;
import android.os.Message;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.fanbai.score.R;
import com.fanbai.score.common.Manager.ActivityManager;
import com.fanbai.score.common.Utils.AppUtils;
import com.fanbai.score.common.Utils.CommonUtil;
import com.fanbai.score.common.Utils.StringUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.Utils.log.YD_Log;
import com.fanbai.score.common.base.BaseActivity;
import com.fanbai.score.common.dialog.MyAlertDialog;
import com.fanbai.score.common.dialog.OnMyAlertDialogClick;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.user.http.UserRequestNetwork_Utils;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by acer on 2017/6/4.
 */

public class Rreg_Activity extends BaseActivity{

    @BindView(R.id.activityRreg_userName)
    public EditText userNameEdit;
    @BindView(R.id.activityRreg_password)
    public EditText passWordEdit;
    @BindView(R.id.activityRreg_vcode)
    public EditText appCodeEdit;
    @BindView(R.id.regActivity_sendAppCode)
    public TextView sendAppCode;
    @BindView(R.id.activityRreg_password_switch)
    ImageButton swithcImg;

    /**
     * 默认隐藏密码输入
     */
    private boolean isShowPw = false;
    @OnClick(R.id.activityRreg_password_switch)
    public void onclickSwitchImg(){
        isShowPw = !isShowPw;
        if(isShowPw){
            swithcImg.setImageResource(R.mipmap.icon_pw_on);
            passWordEdit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//显示密码
        }else{
            swithcImg.setImageResource(R.mipmap.icon_pw_off);
            passWordEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());//隐藏密码
        }
    }
    @OnClick(R.id.register_sendAppCodeFailure)
    public void onClickSendCodeFailure(){
        new MyAlertDialog().showDialog(ctx, "提示", "抱歉，请联系客服处理", new OnMyAlertDialogClick() {
            @Override
            public void onMyAlertDialogClick(boolean is, String... str) {
                CommonUtil.callPhone(ctx, AppUtils.getResource().getString(R.string.service_tel));
            }
        });
    }
    @OnClick(R.id.register_submitBut)
    public void submitRegister(){
        String userName = userNameEdit.getText().toString().trim();
        if(userName == null || userName.equals("")){
            ToastUtils.WarnImageToast(ctx, "手机号不可为空");
            return;
        }
        if(!StringUtils.checkPhoneNumber(userName)){
            ToastUtils.WarnImageToast(ctx, "手机号格式有误");
            return;
        }
        String passWord = passWordEdit.getText().toString().trim();
        if(passWord == null || passWord.equals("")){
            ToastUtils.WarnImageToast(ctx, "密码不可为空");
            return;
        }
        String appCode = appCodeEdit.getText().toString().trim();
        if(appCode == null || appCode.equals("")){
            ToastUtils.WarnImageToast(ctx, "验证码不可为空");
            return;
        }
        showDialog();
        UserRequestNetwork_Utils.userReg(ctx, userName, passWord, appCode, new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                ToastUtils.RightImageToast(ctx, "注册成功"+s);
                hideDialog();
                ActivityManager.getInstance().finishActivity();
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ToastUtils.WarnImageToast(ctx, code+""+msg);
                hideDialog();
            }

            @Override
            public void onError(String code, String msg) {
                ToastUtils.ErrorImageToast(ctx, code+""+msg);
                hideDialog();
            }
        });
    }
    @OnClick(R.id.regActivity_sendAppCode)
    public void setSendAppCode(){
        String userName = userNameEdit.getText().toString().trim();
        if(userName == null || userName.equals("")){
            ToastUtils.WarnImageToast(ctx, "注册手机号不可为空");
            return;
        }
        if(!StringUtils.checkPhoneNumber(userName)){
            ToastUtils.WarnImageToast(ctx, "手机号格式有误");
            return;
        }
        showDialog();
        UserRequestNetwork_Utils.sendVcode(ctx, userName, "reg", new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                hideDialog();
                YD_Log.e("===="+s);
                runTimer();
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                hideDialog();
                ToastUtils.WarnImageToast(ctx, code+""+msg);
            }

            @Override
            public void onError(String code, String msg) {
                hideDialog();
                ToastUtils.ErrorImageToast(ctx, code+""+msg);
            }
        });
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_rreg;
    }

    @Override
    public String getTitleName() {
        return "一键注册";
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public int getTitleLeftName() {
        return R.mipmap.back_click;
    }

    @Override
    public void onClickTitleLeft() {
        ActivityManager.getInstance().finishActivity();
    }

    @Override
    public int getTitleBarColor() {
        return R.color.translucent;
    }

    /**
     * 处理获取验证码倒计时的handler
     */
    Handler iniTime = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            setLavetime(time);
        }
    };
    private Timer timer = null;
    private boolean isTimerRun = false;
    private int time;
    /**
     * 启动获取验证码的倒计时
     */
    private void runTimer() {
        timer = new Timer();
        isTimerRun = true;
        time = 60;
        sendAppCode.setTextColor(getResources().getColor(R.color.color_font_gray));
        setLavetime(time);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                time = time - 1;
                iniTime.sendEmptyMessage(0);
            }
        }, 1000, 1000);
    }
    private void setLavetime(int time) {
        if (time > 0) {
            sendAppCode.setEnabled(false);
            sendAppCode.setText(time + "s后重试");
        } else {
            timer.cancel();
            timer = null;
            isTimerRun = false;
            sendAppCode.setTextColor(getResources().getColor(R.color.colorAccent));
            sendAppCode.setText("获取验证码");
            sendAppCode.setEnabled(true);
        }
    }
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        iniTime.removeCallbacksAndMessages(null);
    }

}
